<?php

    if (!defined('BASEPATH'))
        exit('No direct script access allowed');

    /*
     * Glavna klasa - controler - komunicira sa servisima
     */

    class Main extends CI_Controller {

        public function __construct() {
            parent::__construct();

            // Load the rest client spark
            $this->load->spark('restclient/2.1.0');

            $this->rest->initialize(array(
                'server' => REST_URL,
                'http_user' => 'cipa',
                'http_pass' => 'booking',
                'http_auth' => 'basic' // or 'digest' 
            ));

            $this->activeLang = $this->lang->lang();

            $this->lang->load('common');
        }

        public function index() {
            $this->home();
        }

        /*
         * Customer Form Show Object and room with prices
         * 
         * @params:
         *      check_in
         *      check_out
         *      nr_adults
         *      ages
         *      hotel_id
         *      room_id
         * 
         */

        public function home() {

            $cookie = array(
                'name' => 'my_cookie',
                'value' => '1',
                'expire' => '86500'
            );

            $this->input->set_cookie($cookie);  // Set coockie for redirecting on confirmation page reload 

            $data['object'] = $this->rest->post("customerForm", $_GET);
            $this->load->config('countries');

            $object = currency_convert(array($data['object']));    // Convert currency
            $data['object'] = $object[0];    // Convert currency

            $data['title'] = "Checkout - " . $data['object']->object_name;
            $this->template->set_view('layout_checkout');
            $this->template->load('checkout/step_1', $data);
        }

        /*
         * Customer Form Show Object and room with prices
         * 
         * @params:
         *      check_in
         *      check_out
         *      nr_adults
         *      ages
         *      hotel_id
         *      room_id
         * 
         */

        public function guarantee() {

            $data['object'] = $this->rest->post("customerForm", $_GET);
            $object = currency_convert(array($data['object']));    // Convert currency
            $data['object'] = $object[0];    // Convert currency

            $data['title'] = "Checkout - " . $data['object']->object_name;
            $this->template->set_view('layout_checkout');
            $this->template->load('checkout/step_2', $data);
        }

        public function thanx() {

            $coockie = get_cookie('my_cookie');     // Redirect if page reload
            if (!$coockie)
                redirect(BMNE_URL);
            
             delete_cookie('my_cookie');

            $rest_data = $_GET;

            $data['reservation'] = $this->rest->post("createReservation", $rest_data);
            $data['object'] = $this->rest->post("customerForm", $_GET);

            $object = currency_convert(array($data['object']));    // Convert currency
            $data['object'] = $object[0];    // Convert currency

            $data['title'] = "Checkout - " . $data['object']->object_name;
            $this->template->set_view('layout_checkout');
            $this->template->load('checkout/step_3', $data);
        }

        public function general_terms() {

            $this->data['title'] = "Terms and conditions";
            $this->template->set_view('terms_conditions');
            $this->template->load('checkout/terms_conditions/terms', $this->data);
        }

        public function terms_and_conditions() {

            $this->data['title'] = "Terms and conditions";

            $rest_data = array(
                "hotel_id" => $this->input->get('hotel_id'),
                "room_id" => $this->input->get('room_id'),
                "lang" => $this->lang->lang()
            );

            $this->data['terms_conditions'] = $this->rest->post("termsConditions", $rest_data);
            $this->template->set_view('terms_conditions');
            $this->template->load('checkout/terms_conditions/conditions', $this->data);
        }

    }

?>
